package com.example.sdbtspringboot.service;

import cn.hutool.core.util.RandomUtil;
import com.example.sdbtspringboot.common.Page;
import com.example.sdbtspringboot.entity.User;
import com.example.sdbtspringboot.exception.ServiceException;
import com.example.sdbtspringboot.mapper.UserMapper;
import com.example.sdbtspringboot.utils.TokenUtils;
import jdk.nashorn.internal.parser.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

/**
 * @Description: mapper层用户表
 * @Author: cao
 * @Date: 2024/5/20 9:53
 */
@Service
public class UserService {

    @Autowired
    UserMapper userMapper;

    public void insertUser(User user) {
            userMapper.insert(user);
    }
    public void updateUser(User user) {
            userMapper.update(user);
    }

    public void deleteUser(Integer id) {
        userMapper.delete(id);
    }

    public void batchDeleteUser(List<Integer> ids) {
        for (Integer id : ids) {
            userMapper.delete(id);
        }
    }

    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    public User selectById(Integer id) {
        return userMapper.selectByid(id);
    }

    public List<User> selectByName(String name) {
        return userMapper.selectByName(name);
    }

    public List<User> selectByMore(String name, String username) {
        return userMapper.selectByMore(name,username);
    }

    public List<User> selectByMo(String name, String username) {
        return userMapper.selectByMo(name,username);
    }

    public Page<User> selectByPage(Integer pageNum, Integer pagesize, String name, String username) {
        Integer skipNum =(pageNum-1)*pagesize;

        Page<User> page=new Page<>();
        List<User> userlist = userMapper.selectByPage(skipNum, pagesize, name, username);
        Integer total = userMapper.selectCountByPage(pageNum, pagesize, name,username);
        page.setTotal(total);
        page.setList(userlist);
        return page;
    }

    public User login(User user) {
        User dbUser = userMapper.selectByUserName(user.getUsername());
        if   (dbUser==null){
            throw new ServiceException("用户名或密码错误");
        }
        if (!user.getPassword().equals(dbUser.getPassword())){
            throw new ServiceException("用户名或密码错误");
        }
        String token = TokenUtils.genToken(dbUser.getId().toString(), dbUser.getPassword());
        dbUser.setToken(token);

        return dbUser;
    }

    public User register(User user) {
        User dbUser = userMapper.selectByUserName(user.getUsername());
        if   (dbUser!=null){
            throw new ServiceException("用户名已存在");
        }
//        user.setName("sdbt-"+ RandomUtil.randomNumbers(4));随机生成名字
        user.setName(user.getUsername());

        userMapper.insert(user);
        return user;
    }

    public void resetPassword(User user) {

        User dbUser = userMapper.selectByUserName(user.getUsername());
        if (dbUser==null){
            throw new ServiceException("用户名不存在");
        }
         if (!user.getPhone().equals(dbUser.getPhone())) {
             throw new ServiceException("验证错误，手机号不正确");
        }
         dbUser.setPassword("123");
         userMapper.update(dbUser);

    }
}